package com.aaa.controller.xg;

import com.aaa.pojo.QuFpa;

import com.aaa.pojo.ResultBean;
import com.aaa.service.xg.QuFpaService;

import com.aaa.utils.RetStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;




@RestController
@RequestMapping("/quFpa")
public class QuFpaController {

    @Autowired
    private QuFpaService quFpaService;

    @RequestMapping("/pagination")
    public ResultBean quFpa(String page, String limit){
        int count = quFpaService.findCount();
        List<QuFpa> all = quFpaService.findAll(page, limit);

        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMsg("");
        resultBean.setCount(count);
        resultBean.setData(all);
        //String s = JSON.toJSONString(resultBean);
        return resultBean;

    }

    /**
     * 从前端接受数据 并返回数据到前端
     * @param quFpa
     * @return
     */
    @RequestMapping("/insert")
    public ResultBean insert(@RequestBody QuFpa quFpa){
        quFpa.setCreateTime(new Date());
        quFpa.setUpdateTime(new Date());
        System.out.println(quFpa);
        int insert = quFpaService.insert(quFpa);
       // System.out.println(insert);
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(RetStatusEnum.SUCCESS.getCode());
        resultBean.setMsg(RetStatusEnum.SUCCESS.getMessage());
        resultBean.setData(quFpa);
        resultBean.setCount(insert);
        //String s = JSON.toJSONString(resultBean);
        return resultBean;
    }

    @RequestMapping("/update")
    public ResultBean update(QuFpa quFpa){
        quFpa.setUpdateTime(new Date());
       // System.out.println(quFpa);
        int update = quFpaService.update(quFpa);
        //System.out.println(update);
        ResultBean resultBean = new ResultBean();
        resultBean.setCount(update);
        resultBean.setCode(0);
        resultBean.setData(quFpa);
        resultBean.setMsg("");
        return resultBean;

    }

    @RequestMapping("/delete")
    public ResultBean delete(@RequestParam("idList") List<String> idList){

        //System.out.println(idList);
        int delete = quFpaService.delete(idList);
      //  System.out.println(delete);
        ResultBean resultBean = new ResultBean();
        resultBean.setCode(RetStatusEnum.SUCCESS.getCode());
        resultBean.setMsg(RetStatusEnum.SUCCESS.getMessage());
        return resultBean;

    }

    @RequestMapping("/findIF")
    public ResultBean findIF (String question,String createBy,String page,String limit){
        //System.out.println(question);
        List<QuFpa> quFpaList = quFpaService.findByQuestionAndName(question, createBy,page,limit);
        int ifCount = quFpaService.findIfCount(question, createBy);


        ResultBean resultBean = new ResultBean();
        resultBean.setCode(0);
        resultBean.setMsg("");
        resultBean.setCount(ifCount);
        resultBean.setData(quFpaList);
        return resultBean;

    }
}
